using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class UserRole : BaseItem
    {
        private UserRole() : base() { }
        private UserRole(Hashtable table) : base(table) { }
        private UserRole(Hashtable table, List<UserRole> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new UserRoleDal();
        }

        public static UserRole CreateInstance()
        {
            return new UserRole();
        }

        public static UserRole CreateInstance(int userId, int roleId, int siteId)
        {
            UserRole tmp = new UserRole();
            tmp.userId = userId;
            tmp.roleId = roleId;
            tmp.siteId = siteId;
            return tmp;
        }

        private static UserRole Get(Hashtable table)
        {
            if (table != null)
                return new UserRole(table);
            else
                return null;
        }

        private static List<UserRole> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<UserRole> tmp = new List<UserRole>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new UserRole(table, tmp));
                }
                return tmp;
            }
            else
                return null;
        }

        public static List<UserRole> GetAllInactiveByUserIdSiteId(int userId, int siteId)
        {
            UserRoleDal dal = new UserRoleDal();
            return Get(dal.GetAllInactiveByUserIdSiteId(userId, siteId));
        }

        public static List<UserRole> GetAllActiveByUserIdSiteId(int userId, int siteId)
        {
            UserRoleDal dal = new UserRoleDal();
            return Get(dal.GetAllActiveByUserIdSiteId(userId, siteId));
        }

        public static List<UserRole> GetAllByUserIdSiteIdAndTime(int userId, int siteId, DateTime dateTime)
        {
            UserRoleDal dal = new UserRoleDal();
            return Get(dal.GetAllByUserIdSiteIdAndTime(userId, siteId, dateTime));
        }

        public static UserRole GetById(int id)
        {
            UserRoleDal dal = new UserRoleDal();
            return Get(dal.GetById(id));
        }

        public new void Update()
        {
            base.Update();
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
            if (IsNewRecord)
            {
                created = DateTime.Now;
            }
            ((UserRoleDal)dal).Update(ref id, userId, ref userName, ref firstName, ref lastName, roleId, ref roleName, ref isAdminRole, siteId, created, start, end, ref concurencyId);
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[UserRoleDal.Fields.Id];
            userId = (int)table[UserRoleDal.Fields.UserId];
            userName = (string)table[UserRoleDal.Fields.UserName];
            firstName = (string)table[UserRoleDal.Fields.FirstName];
            lastName = (string)table[UserRoleDal.Fields.LastName];
            roleId = (int)table[UserRoleDal.Fields.RoleId];
            roleName = (string)table[UserRoleDal.Fields.RoleName];
            isAdminRole = (bool)table[UserRoleDal.Fields.IsAdminRole];
            siteId = (int)table[UserRoleDal.Fields.SiteId];
            created = (DateTime)table[UserRoleDal.Fields.Created];
            start = (DateTime)table[UserRoleDal.Fields.Start];
            end = (DateTime?)table[UserRoleDal.Fields.End];
        }

        private int id;
        public int Id
        {
            get { return id; }
        }

        private int userId;
        public int UserId
        {
            get { return userId; }
        }

        private string userName;
        public string UserName
        {
            get { return userName; }
        }

        private string firstName;
        public string FirstName
        {
            get { return firstName; }
        }

        private string lastName;
        public string LastName
        {
            get { return lastName; }
        }
        
        private int roleId;
        public int RoleId
        {
            get { return roleId; }
        }

        private string roleName;
        public string RoleName
        {
            get { return roleName; }
        }

        private bool isAdminRole;
        public bool IsAdminRole
        {
            get { return isAdminRole; }
        }

        private int siteId;
        public int SiteId
        {
            get { return siteId; }
        }
	
        private DateTime created;
        public DateTime Created
        {
            get { return created; }
        }

        private DateTime start = DateTime.Now;
        public DateTime Start
        {
            get { return start; }
            set { start = value; start.Subtract(new TimeSpan(0, 0, 0, start.Second)); }
        }

        private DateTime? end;
        public DateTime? End
        {
            get { return end; }
            set { end = value; }
        }

        internal bool IsRole(Role.Roles role)
        {
            if (role == Role.Roles.Administrator)
            {
                return this.IsAdminRole;
            }
            else
            {
                return this.RoleId == (int)role;
            }
        }

    }
}
